--[[
FilePath: \Lua\View\DungeonRandomEventUI\UIRandomEventHero.lua
主线随机事件--租借英雄
--]]

require 'View/UIBase/UIBase'

UIRandomEventHero = UIBase.New(PanelResNames.UIRandomEventHero)
local _M = UIRandomEventHero

_M.hasInit = false
_M.heroModelId = 0;

function _M.Init(func)
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Btn_BgClose"), self.OnBack)
        self:RegisterOnClick(self:GetChild("Btn_Confirm"), self.OnConfirm)
        self.textHelpTips = self:GetChild("Text_HelpTips");
        self.textPower = self:GetChild("Text_Power");
        self.textHeroTalk = self:GetChild("Text_HeroTalk");
        self.heroPos = self:GetChild("Graph_HeroPos");

        self.enemyRole = nil;
        _M.hasInit = true

        self:addEvent();
    end
end

function _M:addEvent()
    self:RegisterEvent(Config.EventType.Fresh_DungeonRandomEventAccept,self.updateView);
end

function _M:removeEvent()
    self:UnRegisterEvent(Config.EventType.Fresh_DungeonRandomEventAccept,self.updateView);
end

-- 显示
function _M:OnEnable()
    if not self.hasInit then
        self:OnBack();
        return;
    end
    self:refresh();
end

function _M:refresh()
    self.data = logicMgr.DungeonRandomEventManager.getDungeonRandomEventData();
    if self.data == nil or self.data.partner == nil or HelperFunc.GetServerTime() > self.data["overdueTime"] then
        self:OnBack();
        return;
    end

    self.heroModelId = self.data.partner.partnerModelId;
    local interactionId = self.heroModelId .. "002";
    local interactionInfo = G.dataTable["t_interaction"][tostring(interactionId)]
    if interactionInfo ~= nil then
        self.textHeroTalk.text = getLanguage(interactionInfo.f_LineText);
    else
        self.textHeroTalk.text = "";
    end
    local heroCfg = G.dataTable["t_hero"][tostring(self.heroModelId)];
    if heroCfg ~= nil then
        local heroName = getLanguage(heroCfg.f_HeroName);
        self.textHelpTips.text = string.format(getLanguage("RandomEventHero_Tips"), heroName, heroName);
    else
        self.textHelpTips.text = string.format(getLanguage("RandomEventHero_Tips"), "", "");
    end

    local path = HeroManager.GetHeroDrawingRolePath(self.heroModelId);
    resMgr:LoadModelPrefab(path, function(go)
        if self.enemyRole ~= nil then
            destroyImmediate(self.enemyRole);
            self.enemyRole = nil
        end
        self.enemyRole = newObject(go);
        self.enemyRole.name = path;
        self.enemyRole:AddComponent(TCanvas)
        if self.enemyWrapper == nil then
            self.enemyWrapper = GoWrapper.New()
        end
        self.enemyWrapper:SetWrapTarget(self.enemyRole,false);
        Util.Lua_SetLocalPositionXYZ(self.enemyRole, 60, -300, 0);
        Util.Lua_SetLocalScaleXYZ(self.enemyRole, 130, 130, 130);
        self.heroPos:SetNativeObject(self.enemyWrapper);
        local enemyObj = Util.Lua_FindGameObject(self.enemyRole, "Animation");
        logicMgr.EquipManager.setWeaponSkin(enemyObj, self.heroModelId, true);
        Util.PlayAction(enemyObj, "fidle");
    end);

    self:updateView();
end

function _M:updateView(reward)
    self.isBuy = false;
    if reward ~= nil then
        local itemId = 201500 + self.heroModelId
        logicMgr.DungeonRandomEventManager.getFetchReward({{itemModelId = itemId}}, function()
            self:OnBack();
        end);
    end
end

function _M:OnConfirm()
    if self.isBuy then
        return;
    end
    -- 判断是否已有租借英雄
    if true then
        local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop)
        pop.Init(102, function(flag)
            if flag then
                self:DungeonRandomEventAccept();
            end
        end);
    else
        self:DungeonRandomEventAccept();
    end
end

function _M:DungeonRandomEventAccept()
    if HelperFunc.GetServerTime() > self.data["overdueTime"] then
        UISysTips.AddMsg(getLanguage("ErrorCode_210004"));
        self:OnBack();
    else
        self.isBuy = true;
        DungeonRandomEventHandler.sendDungeonRandomEventAccept();
    end
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    if self.enemyRole ~= nil then
        destroy(self.enemyRole);
        self.enemyRole = nil;
    end
    if self.enemyWrapper then
        self.enemyWrapper:Dispose();
        self.enemyWrapper = nil;
    end
    self:removeEvent();
    self.hasInit = false
end

return _M;